package com.brook.app.android.filepicker.core;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.brook.app.android.filepicker.FilePickerUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Brook
 * @time 2020-03-14 10:56
 */
public class FilePickerConfig implements Cloneable {

    private static final String TAG = "FilePickerConfig";

    private Set<IFileProvider> mFileProviders;
    private String mPickerMimeType;
    private FilePickCallback mPickCallback;
    private IStartPickerUIDelegate mStartPickerUIDelegate;
    private IThumbnailLoader mImageLoader;
    private Context mApplicationContext;
    private int mPickCount = 1;
    private long mFileSize = Long.MAX_VALUE;

    public FilePickerConfig() {
        mFileProviders = new HashSet<>();
        mPickerMimeType = MimeType.ALL.toString();
    }

    public long getFileSize() {
        return mFileSize;
    }

    public FilePickerConfig setFileSize(long kb) {
        this.mFileSize = kb;
        return this;
    }

    public int getPickCount() {
        return mPickCount;
    }

    public FilePickerConfig setPickCount(int pickCount) {
        this.mPickCount = pickCount;
        return this;
    }

    public FilePickerConfig addFileProvider(IFileProvider provider) {
        this.mFileProviders.add(provider);
        return this;
    }

    public String getPickerMimeType() {
        return mPickerMimeType;
    }

    public FilePickerConfig setPickerMimeType(String type) {
        String[] mimeType = type.split("/");
        if (mimeType.length != 2) {
            throw new IllegalArgumentException("mineType error");
        }
        this.mPickerMimeType = type;
        return this;
    }

    public Context getApplicationContext() {
        return mApplicationContext;
    }

    public FilePickerConfig setApplicationContext(@NonNull Context mContext) {
        this.mApplicationContext = mContext;
        return this;
    }

    @Nullable
    public IThumbnailLoader getImageLoader() {
        return mImageLoader;
    }

    public FilePickerConfig setImageLoader(IThumbnailLoader imageLoader) {
        this.mImageLoader = imageLoader;
        return this;
    }

    public IStartPickerUIDelegate getStartPickerUIDelegate() {
        return mStartPickerUIDelegate;
    }

    public FilePickerConfig setStartPickerUIDelegate(IStartPickerUIDelegate startPickerUIDelegate) {
        this.mStartPickerUIDelegate = startPickerUIDelegate;
        return this;
    }

    public FilePickCallback getCallback() {
        return mPickCallback;
    }

    public FilePickerConfig setCallback(FilePickCallback callback) {
        this.mPickCallback = callback;
        return this;
    }


    public FilePickerConfig setFileProviders(Set<IFileProvider> providers) {
        mFileProviders = providers;
        return this;
    }

    public Set<IFileProvider> getProviders() {
        return mFileProviders;
    }

    @Nullable
    public IFileProvider getProvider(String mineType) {
        for (IFileProvider provider : mFileProviders) {
            if (provider.handleMimeType(mineType)) {
                provider.injectConfig(this);
                return provider;
            }
        }
        return null;
    }

    public void startLaunchPickerUI(Context context) {
        if (mStartPickerUIDelegate != null) {
            mStartPickerUIDelegate.launchPickerUI(context, this);
        }
    }

    @NonNull
    @Override
    public FilePickerConfig clone() throws CloneNotSupportedException {
        return (FilePickerConfig) super.clone();
    }


    public FilePickerUtils build() {
        return new FilePickerUtils(this);
    }

    public void resetBy(FilePickerConfig defaultConfig) {
        setImageLoader(defaultConfig.getImageLoader());
        setStartPickerUIDelegate(defaultConfig.getStartPickerUIDelegate());
        setFileProviders(defaultConfig.getProviders());
    }

}